<?php
    class product_hotProduct_Controller extends controllerBase
    {
        public function process()
        {
            database::isUser(true);
            $oDatabase = new database();
            $sQuery = "
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX bus: <".URL::getLink('id/bussiness#').">

            SELECT *     
            WHERE 
            {
            ?uri rdf:type bus:product.
            ?uri bus:name ?name.
            ?uri bus:ownUser ?userUri.
            ?userUri foaf:familyName ?userName.
            ?uri bus:hasCategory ?categoryUri.
            ?categoryUri bus:name ?categoryName.
            ?uri bus:defaultImage ?image.".
            (!database::isAdmin() ? "FILTER(?ownUser = '".$_SESSION['user_uri']."')." : '')
            ."}";

            $aResult = $oDatabase->loadQuery($sQuery);
            if(!empty($aResult['result']['rows']))
            {
                $this->aParams['aProducts'] = $aResult['result']['rows'];
            }

            // Get hot product
            if(database::isAdmin())
            {
                $uri = URL::getLink('id/hotProduct');
            }
            else
            {
                $uri = URL::getLink('id/'.$_SESSION['user_uri'].'_hotProduct');
            }
            $sQuery = "
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            PREFIX bus: <".URL::getLink('id/bussiness#').">

            SELECT DISTINCT *  
            WHERE 
            {
            ?uri bus:hasProduct ?product.
            ?product bus:name ?name.
            ?product bus:ownUser ?userUri.
            ?userUri foaf:familyName ?userName.
            ?product bus:hasCategory ?categoryUri.
            ?categoryUri bus:name ?categoryName.
            OPTIONAL{?product bus:hotImage ?image.}
            OPTIONAL{?product bus:defaultImage ?defaultImage.}
            FILTER(?uri = '".$uri."').
            }";
            
            $aResult = $oDatabase->loadQuery($sQuery);
            if(!empty($aResult['result']['rows']))
            {
                $this->aParams['aHotProducts'] = $aResult['result']['rows'];
            }
        }
    }
?>
